package com.zp.drug.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.common.core.constant.CacheNames;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.common.redis.utils.CacheUtils;
import com.zp.drug.entity.DrugStorageDept;
import com.zp.drug.entity.DrugSupplierCata;
import com.zp.drug.entity.bo.DrugSupplierCataBo;
import com.zp.drug.entity.vo.DrugSupplierCataVo;
import com.zp.drug.mapper.DrugSupplierCataMapper;
import com.zp.drug.service.IDrugSupplierCataService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 药品供应商生产商维护Service业务层处理
 *
 * @author zhang peng
 * @ date 2023-05-31
 */
@RequiredArgsConstructor
@Service
public class DrugSupplierCataServiceImpl implements IDrugSupplierCataService {

    private final DrugSupplierCataMapper baseMapper;

    /**
     * 查询药品供应商生产商维护
     */
    @Override
    public DrugSupplierCataVo queryById (String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询药品供应商生产商维护列表(分页)
     */
    @Override
    public TableDataInfo<DrugSupplierCataVo> queryPageList (DrugSupplierCataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DrugSupplierCata> lqw = buildQueryWrapper(bo);
        Page<DrugSupplierCataVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询药品供应商生产商维护列表(不分页)
     */
    @Override
    public List<DrugSupplierCataVo> queryList (DrugSupplierCataBo bo) {
        LambdaQueryWrapper<DrugSupplierCata> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DrugSupplierCata> buildQueryWrapper (DrugSupplierCataBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DrugSupplierCata> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSupplierJc()), DrugSupplierCata::getSupplierJc, bo.getSupplierJc());
        lqw.like(StringUtils.isNotBlank(bo.getSupplierName()), DrugSupplierCata::getSupplierName, bo.getSupplierName());
        lqw.eq(StringUtils.isNotBlank(bo.getSupplierClass()), DrugSupplierCata::getSupplierClass, bo.getSupplierClass());
        lqw.eq(StringUtils.isNotBlank(bo.getInputCode()), DrugSupplierCata::getInputCode, bo.getInputCode());
        lqw.eq(StringUtils.isNotBlank(bo.getTrademark()), DrugSupplierCata::getTrademark, bo.getTrademark());
        lqw.eq(StringUtils.isNotBlank(bo.getForeignx()), DrugSupplierCata::getForeignx, bo.getForeignx());
        lqw.eq(StringUtils.isNotBlank(bo.getSupplierCode()), DrugSupplierCata::getSupplierCode, bo.getSupplierCode());
        lqw.eq(StringUtils.isNotBlank(bo.getUsedFlag()), DrugSupplierCata::getUsedFlag, bo.getUsedFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), DrugSupplierCata::getRemarks, bo.getRemarks());
        return lqw;
    }

    /**
     * @return
     * @Author zhangpeng
     * @Description 校验前台传入的数据是否存在
     * @ date 21:18 2023/6/7
     * @Param * @param bo
     **/
    public DrugSupplierCata getSupplierCata (DrugSupplierCataBo bo) {
        DrugSupplierCata old = new DrugSupplierCata();
        old = baseMapper.selectOne(Wrappers.<DrugSupplierCata>lambdaQuery()
                .eq(StringUtils.isNotBlank(bo.getSupplierName()), DrugSupplierCata::getSupplierName, bo.getSupplierName())
                .eq(StringUtils.isNotBlank(bo.getSupplierClass()), DrugSupplierCata::getSupplierClass, bo.getSupplierClass())
        );
        return old;
    }

    /**
     * 新增药品供应商生产商维护
     */
    @Override
    public DrugSupplierCataBo insertByBo (DrugSupplierCataBo bo) {
        DrugSupplierCata old = this.getSupplierCata(bo);
        if (ObjectUtils.isNotEmpty(old)) {
            throw new RuntimeException("厂商已存在，请修改名称后再保存");
        }
        DrugSupplierCata add = BeanUtil.toBean(bo, DrugSupplierCata.class);
        validEntityBeforeSave(add);
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        CacheUtils.put(CacheNames.DRUG_SUPPLIER_CATA, add.getId(), add);
        return bo;
    }

    /**
     * 修改药品供应商生产商维护
     */
    @Override
    public Boolean updateByBo (DrugSupplierCataBo bo) {
        DrugSupplierCata old = this.getSupplierCata(bo);
        if (ObjectUtils.isNotEmpty(old) && !StringUtils.equals(old.getId(), bo.getId())) {
            throw new RuntimeException("厂商已存在，请修改名称后再保存");
        }
        DrugSupplierCata update = BeanUtil.toBean(bo, DrugSupplierCata.class);
        validEntityBeforeSave(update);
        boolean b = baseMapper.updateById(update) > 0;
        CacheUtils.evict(CacheNames.DRUG_SUPPLIER_CATA, update.getId());
        CacheUtils.put(CacheNames.DRUG_SUPPLIER_CATA, update.getId(), update);
        return b;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (DrugSupplierCata entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除药品供应商生产商维护
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        boolean b = baseMapper.deleteBatchIds(ids) > 0;
        ids.forEach(item->{
            CacheUtils.evict(CacheNames.DRUG_SUPPLIER_CATA,item);
        });
        return b;
    }

    /**
     * 刷新缓存（药品供应商生产商）
     */
    @Override
    public void refreshCacheDrugSupplierCata () {
        List<DrugSupplierCata> list = baseMapper.selectList();
        list.forEach(item -> {
            CacheUtils.put(CacheNames.DRUG_SUPPLIER_CATA,item.getId(),item);
        });
    }
}
